package edu.jptest.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

import edu.jptest.bean.QuestionType;
import edu.jptest.service.IQuestionTypeService;
import edu.jptest.serviceImp.QuestionTypeService;

public class QuestionTypeAction extends ActionSupport {
	private static final long serialVersionUID = 1577337271845614277L;
	private QuestionType questionType;
	private List<QuestionType> questionTypeList;
	private List<String> typeList;
	private List<String> contentList;
	private Map<String, List<String>> questionTypeMap;

	public Map<String, List<String>> getQuestionTypeMap() {
		return questionTypeMap;
	}

	public void setQuestionTypeMap(Map<String, List<String>> questionTypeMap) {
		this.questionTypeMap = questionTypeMap;
	}

	public List<String> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<String> typeList) {
		this.typeList = typeList;
	}

	// 根据select的值取得content列表
	public List<String> getContentList() {
		ValueStack stack = ServletActionContext
				.getValueStack(ServletActionContext.getRequest());
		Object types = stack.findValue("top");
		if (types != null) {
			contentList = (List<String>) questionTypeMap.get(types);
			return contentList;
		} else {
			return Collections.EMPTY_LIST;
		}
	}

	public void setContentList(List<String> contentList) {
		this.contentList = contentList;
	}

	public List<QuestionType> getQuestionTypeList() {
		return questionTypeList;
	}

	public void setQuestionTypeList(List<QuestionType> questionTypeList) {
		this.questionTypeList = questionTypeList;
	}

	public QuestionType getQuestionType() {
		return questionType;
	}

	public void setQuestionType(QuestionType questionType) {
		this.questionType = questionType;
	}

	// 得到题型列表
	public String getQuestionTypes() {
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		// questionTypeList = new ArrayList<QuestionType>();
		questionTypeList = iQuestionTypeService.getAllQuestionTypes();
		return SUCCESS;
	}

	// 添加题型
	public String addQuestionType() {
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		System.out.println("==============");
		System.out.println(questionType.getContent() + " "
				+ questionType.getType());
		if (iQuestionTypeService.insertQuestionType(questionType)) {
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	// 进入修改题型页面
	public String modifyQuestionTypeInfo() {
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		questionType = iQuestionTypeService
				.getquestionTypeByquestionTypeId(questionType
						.getQuestionTypeId());
		return SUCCESS;
	}

	// 修改题型
	public String modifyQuestionType() {
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		if (iQuestionTypeService.updateQuestionType(questionType))
			return SUCCESS;
		else {
			return INPUT;
		}
	}

	// 删除题型
	public String deleteQuestionType() {
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		if (iQuestionTypeService.deleteQuestionType(questionType
				.getQuestionTypeId()))
			return SUCCESS;
		else
			return INPUT;
	}

	// 添加试题选择教材、题型，得到题型列表
	public String getQuestionTypeSelect() {
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		questionTypeList = iQuestionTypeService.getAllQuestionTypes();
		Iterator<QuestionType> it = questionTypeList.iterator();

		typeList = new ArrayList<String>();
		questionTypeMap = new HashMap<String, List<String>>();

		while (it.hasNext()) {
			QuestionType type = (QuestionType) it.next();
			if (!typeList.contains(type.getType())) {
				typeList.add(type.getType());
				List<String> contents = new ArrayList<String>();
				contents.add(type.getContent());
				questionTypeMap.put(type.getType(), contents);
			} else {
				List<String> contents = questionTypeMap.get(type.getType());
				if (contents == null) {
					List<String> contents2 = new ArrayList<String>();
					contents2.add(type.getContent());
					questionTypeMap.put(type.getType(), contents2);
				} else {
					contents.add(type.getContent());
					questionTypeMap.put(type.getType(), contents);
				}
			}
		}
		return SUCCESS;
	}
}
